package com.songlanyun.common.utils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

import javax.annotation.Resource;

//编程事务（需要手动begin 手动回滚  手都提交）
@Component
public class TransactionUtils {

    // 获取事务源
    @Resource
    private PlatformTransactionManager platformTransactionManager;

    // 开启事务
    public TransactionStatus begin() {
        TransactionStatus transaction = platformTransactionManager.getTransaction(new DefaultTransactionAttribute());
        return transaction;
    }

    // 提交事务
    public void commit(TransactionStatus transaction) {
        platformTransactionManager.commit(transaction);
    }

    // 回滚事务
    public void rollback(TransactionStatus transaction) {
        platformTransactionManager.rollback(transaction);
    }

}